#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib || exit 1

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

echo "keyboard-configuration	keyboard-configuration/variant	select	English (US)" | debconf-set-selections

pkg update

# install packages
if [ -f $SCRIPT_DIR/$PACKAGE_VARIANT.list ]; then
    pkg install \
        $(grep -vE "^\s*#" $SCRIPT_DIR/$PACKAGE_VARIANT.list | tr "\n" " ")
fi

if [ $DESKTOP_ENVIRONMENT = "flux" ]; then
    USER_NAME="root"
fi
cat <<EOF >/usr/lib/systemd/system/xorg.service
[Unit]
Description=X-Window
ConditionKernelCommandLine=!text
After=systemd-user-sessions.service

[Service]
ExecStart=/bin/su --login -c "/usr/bin/startx -- :0 vt7 -ac -nolisten tcp" $USER_NAME

EOF

if [ -L /etc/systemd/system/display-manager.service ]; then
    echo "Display manager link exists."
else
    mkdir -p /etc/systemd/system
    ln -s /usr/lib/systemd/system/xorg.service /etc/systemd/system/display-manager.service
fi
